// export function apiRequest(options) {  
//     return new Promise((resolve, reject) => {  
//         const requestOptions = {  
//             url: options.url,  
//             method: options.method || "GET",  
//             data: options.data || {},  
//             header: options.header || {},  
//             success: (res) => {  
//                 if (res.statusCode === 200) {  
//                     resolve(res.data);  
//                 } else {  
//                     reject(res);  
//                 }  
//             },  
//             fail: (err) => {  
//                 reject(err);  
//             },  
//         };  
//         uni.request(requestOptions);  
//     });  
// }

import {baseURL,baseURL1} from './config.js'; 

let BASEURL = baseURL // 调整当前环境
let BASEURL2 = baseURL1 // 调整当前环境
let contentType = {
	urlencoded:'application/x-www-form-urlencoded;charset:utf-8;',
	json:'application/json',
}
/*
* 全局请求封装
* @param path 请求路径
* @param method 请求类型(GET/POST/DELETE等)
* @oaram data 请求体数据
* @oaram hasToken 是否带token
* @oaram type content-type
* @param loading 请求未完成是是否显示加载中，默认为true
*/
export default (path, method, data = {},hasToken = true,type = 'urlencoded', loading = true,) => {
	let token = undefined
	if(hasToken){
		// 获取存储token
		token = uni.getStorageSync("TOKEN");
	}
	if (loading) {
		uni.showLoading({
			title: "加载中",
			mask: true
		});
	};
	//根据token进行调用函数
	if (hasToken) {
		return tokenRequest(path, method, data,token,type, loading)
	} else {
		return noTokenRequest(path, method, data,type, loading)
	}
};

// 无token时发送请求函数
function noTokenRequest(path, method, data,type, loading) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASEURL2 + path,
			method: method,
			data,
			header:{
				'content-type': contentType[type]
			},
			success(response) {
				// console.log('%c响应拦截：', ' background:green', response);
				/* if (response.data.code === 3001) {
					// logout()
				} */
				/* if (response.data.code !== 20) {
					uni.showToast({
						icon: "none",
						duration: 4000,
						title: response.data.msg
					});
				} */
				console.log(response.data)
				resolve(response.data);
			},
			fail(err) {
				uni.showToast({
					icon: "none",
					title: '服务响应失败'
				});
				console.error(err);
				reject(err);
			},
			complete() {
				uni.hideLoading({
				
				fail() {}
				
				});
				
				// uni.hideLoading();
			}
		});
	});
}


// 有token时发送请求函数
function tokenRequest(path, method, data,token, type, loading ) {
	return new Promise((resolve, reject) => {
		urlRequest(path, method, data, token, type, loading)
	});
}
function urlRequest(path, method, data, token, type, loading) {
	uni.request({
		url: BASEURL + path,
		method: method,
		data,
		header: {
			'content-type': contentType[type],
			"Authorization": token
		},
		success(response) {
			// console.log('%c响应拦截：', ' background:green', response);
			if (response.data.code === 40101) {
				// logout()
			}
			// console.log(response.data)
			resolve(response.data);
		},
		fail(err) {
			uni.showToast({
				icon: "none",
				title: '服务响应失败'
			});
			console.error(err);
			reject(err);
		},
		complete() {
			uni.hideLoading();
		}
	});
}
